package GUILayer;

import ControlLayer.OrdreController;
import ControlLayer.VareController;
import ModelLayer.DelOrdreModel;
import ModelLayer.OrdreCollectionModel;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class OrdreUI {

    //Properties
    private JFrame frame;
    private JPanel MainWindow;
    private JPanel panelVarer;
    private JPanel panelKunde;
    private JPanel panelOrdre;
    private JPanel panelKnap;
    private JTextField txtKundeNavn;
    private JTextField txtKundeAdresse;
    private JTextField txtKundeTelefon;
    private JTextField txtOrdreNr;
    private JTextField txtOrdreDato;
    private JTextField txtOrdreStatus;
    private JButton btnOpret;
    private JButton btnAnnuller;
    private JTextField txtTotal;
    private JLabel lblTotal;
    private JLabel lblKundeNavn;
    private JLabel lblKundeAdresse;
    private JLabel lblKundeTelefon;
    private JLabel lblOrdreNr;
    private JLabel lblOrdreDato;
    private JLabel lblOrdreStatus;
    private JTable tableVarer;

    private OrdreController ordreController;
    private OrdreCollectionModel ordreCollectionModel;
    private VareController vareController;

    public static void main(String[] args) {
        JFrame frame = new JFrame("Vestbjerg Byggecenter A/S");
        frame.setContentPane(new OrdreUI().MainWindow);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setBounds(100, 100, 640, 500);
        frame.pack();
        frame.setVisible(true);
    }

    //Constructor
    public OrdreUI() {
        //Initialiserer controllerlaget
        ordreController = new OrdreController();
        vareController = new VareController();

        //Tilføjer varer til delordren
        tilføjVarer();

        //Viser delordren i GUI'en
        opretVareTabel();

        //Udregner en samlet pris
        samletPris();

       //Opret ordre
        btnOpret.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    opretOrdre();
                }
                //Oplyser om fejl ved tal indtastning
                catch (NumberFormatException nfe) {
                    JOptionPane.showMessageDialog(frame, "Telefon og/eller ordre nr. er ikke gyldige tal");
                }
            }
        });
    }


    //Opretter en ordre ud fra de indtastede informationer i tekstfelterne (JTextField)
    public void opretOrdre(){
        ordreController.opretOrdre(Integer.parseInt(txtOrdreNr.getText()), txtOrdreDato.getText(), txtOrdreStatus.getText());

        ordreCollectionModel.getInstance().printOrdre();

        System.out.println("Leverings oplysninger:");
        System.out.println("Navn: " + txtKundeNavn.getText());
        System.out.println("Adresse: " + txtKundeAdresse.getText());
        System.out.println("Telefon: " + Integer.parseInt(txtKundeTelefon.getText()));


        System.out.println("\nVarer: ");
        printDelOrdre();


        JOptionPane.showMessageDialog(frame, "Ordren er oprettet");
    }

    public void printDelOrdre(){
        ordreCollectionModel.getInstance().printDelOrdre();

    }

    public void tilføjVarer(){
        ordreController.tilføjVare(11111, 5);
        ordreController.tilføjVare(11112, 10);
        ordreController.tilføjVare(11113, 2);
        ordreController.tilføjVare(11114, 1);
        ordreController.tilføjVare(11115, 3);
    }

    private void opretVareTabel()
    {
        ArrayList<DelOrdreModel> delOrdreListe = ordreController.getDelOrder();
        TableModel model = new TableModel();
        model.setData(delOrdreListe);
        tableVarer.setModel(model);
    }

    private void samletPris(){
        txtTotal.setText(Double.toString(ordreController.getTotalPris()));
    }


}

